import { createParamDecorator, ExecutionContext } from '@nestjs/common';

/**
 * @ReqUser装饰器,用来获取req.user
 * 用法参考:https://docs.nestjs.com/custom-decorators
 * 使用方法 @ReqUser() user:xxx
 */
export const ReqUser = createParamDecorator(
  (data: string, ctx: ExecutionContext) => {
    const request = ctx.switchToHttp().getRequest();
    const user = request.user;

    return data ? user?.[data] : user;
  },
);
